package com.qixin.part.controller;


import com.alibaba.fastjson.JSONObject;
import com.qixin.api.bean.ComPtUser;
import com.qixin.api.bean.ResultPage;
import com.qixin.api.bean.VoPage;
import com.qixin.api.service.ComPtUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.Date;

/**
 * <p>
 * 企业用户表 前端控制器
 * </p>
 *
 * @author 吕正睿
 * @since 2018-10-28
 */
@Controller
@RequestMapping("/comPtUser")
public class ComPtUserController {
    private Logger logger = LoggerFactory.getLogger(ComPtUserController.class);
    @Autowired
    ComPtUserService comPtUserService;


    /**
     *跳转主页
     */
    @RequestMapping("/main")
    public String main(  HttpSession session){

        return "partIndex";
    }
    /**
     *跳转密码管理
     */
    @RequestMapping("/password.html")
    public String password(  ){ return "/company/password_manager"; }
    /**
     *跳转登录
     */
    @RequestMapping("/login.html")
    public String login(  ){ return "/system/login"; }
//	/**
//	 *跳转
//	 */
//    @RequestMapping("/register.html")
//    public String register(  ){ return "/system/register"; }
    /**
     *跳转企业认证
     */
    @RequestMapping("/Enterprise.html")
    public String Enterprise(Model model ,HttpSession session){
		Long id=((ComPtUser)session.getAttribute("user")).getId();
        String license=comPtUserService.license(id);
        model.addAttribute("license",license);
        return "/company/Enterprise"; }
	//跳转系统管理的人员管理页面

	@RequestMapping("/userAdmin")
	public String userAdmin(ComPtUser comPtUser, HttpSession session){
		return  "/system/user";
	}
	@RequestMapping("/user_add")
	public String user_add (){
		return "/system/user_add";
	}
    /**
     *欢迎页
     */
    @RequestMapping("/welcome.html")
     public String welcome( Model model,String ptname,Long id ){
        System.out.println(ptname+id);
        int peoplenum=comPtUserService.sepeople(id);
        int chargerecord=comPtUserService.chargenum(id);
        long carnum=comPtUserService.carnum(ptname);
        model.addAttribute("num",peoplenum);
        model.addAttribute("num2",chargerecord);
        model.addAttribute("num3",carnum);
        return "/system/welcome";
    }
	//跳转 首页
	@RequestMapping("/findMenu")
	@ResponseBody
	public ComPtUser findMenu(HttpSession session){
		ComPtUser comPtUser=new ComPtUser();
		comPtUser=(ComPtUser) session.getAttribute("user");
		return comPtUser;
	}
    /*
    登录
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public boolean login(ComPtUser comPtUser, HttpSession session) {
        if (comPtUserService.login(comPtUser, session)){

            return true;
        }
        return false;
    }

    /*
    密码管理
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public boolean update(ComPtUser comPtUser,HttpSession session){
        if (comPtUserService.update(comPtUser,session)){
            return true;
        }
        return false;
    }
    //    @RequestMapping(value = "sepeople", method = RequestMethod.POST)
//    @ResponseBody
//    public int sepeople(ComPtUser comPtUser, Model model) {
//        int num=comPtUserService.sepeople();
//        System.out.println("num=  "+num);
//        model.addAttribute("num",num);
//
//        return num;
//
//
//    }
    //更新认证信息（comptuser表）
    @RequestMapping("/checkup")
    @ResponseBody
    public String insert(String param,Long[] filesId) {
        logger.info(param);
        ComPtUser comPtUser= JSONObject.parseObject(param,ComPtUser.class);
        comPtUser.setCreateTime(new Date().getTime());
        try {
            return    comPtUserService.checkup(comPtUser,filesId);
        } catch (SQLException e) {
            e.printStackTrace();
            return "保存出现意外，有图片丢失。";
        }

    }
    //更新企业执照前的删除
    @RequestMapping("/mov")
    @ResponseBody
    public boolean license( Long id){
        int a=comPtUserService.mov(id);
        if (a>0){
            return true;
        }
        return   false;


    }


}
